package com.hh.demo_ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hh.listeners.OnActionBarClickListener;
import com.hh.listeners.OnPopupMenuItemClickListener;
import com.hh.ui.widget.UiActionBar;
import com.hh.ui.widget.UiPopupMenu;
import com.hh.ui.widget.UiPopupMenu.UIPopUpMenu_ALIGN;

public class Sample_ActionBar extends Activity implements OnActionBarClickListener {


	private final int ID_CANCEL=1000;
	private final int ID_DONE=1001;
	private final int ID_MENU=1002;
	private final int ID_USER=1003;
	
	
	private Button lBtnMenu;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sample_actionbar);


		/**
		 * Action bar on the top of the screen
		 */
		// create action bar
		UiActionBar myActionTop=(UiActionBar) findViewById(R.id.myActionBarTop);

		// create button "Menu"
		lBtnMenu=myActionTop.createActionChild(ID_MENU, "",getResources().getDrawable( R.drawable.ic_action_menu));
		//Add button to the action bar : Right side
		myActionTop.addChildActionRight(lBtnMenu);

		TextView lTitle =myActionTop.createActionBarTitle("Test Action Bar",100,0);
		
		lTitle.setTextSize(20);
		lTitle.setTextColor(Color.BLUE);
		myActionTop.addChildActionLeft(lTitle);
		
		//Add listener to handle childs action
		myActionTop.setOnActionBarClickListener(this);


		/**
		 * Action bar on the bottom of the screen
		 */
		// create action bar
		UiActionBar myActionBarBotttom=(UiActionBar) findViewById(R.id.myActionBarBottom);

		/*
		 * Method 1 : add the action bar button using "addChildActionLeft" method
		 */
		//Add button to the action bar : Left side
		myActionBarBotttom.addChildActionLeft(ID_DONE, "done", getResources().getDrawable( R.drawable.ic_action_done));
			
		myActionBarBotttom.addChildActionLeft(ID_USER, "User", getResources().getDrawable( R.drawable.ic_action_user));

		/*
		 * Method 2 : Create the button THEN add it to the action bar, in this case we can customize 
		 * the action button : color , textSize etc...
		 * 
		 */
		
		// create button "Cancel"
		Button lBtnCancel=myActionBarBotttom.createActionChild(ID_CANCEL, "Cancel",getResources().getDrawable( R.drawable.ic_action_cancel));
		// Customize text color
		lBtnCancel.setTextColor(Color.RED);
		
		//Add button to the action bar : Right side
		myActionBarBotttom.addChildActionRight(lBtnCancel);

		//Add listener to handle childs action
		myActionBarBotttom.setOnActionBarClickListener(this);



	}

	private void showMenu(){
		
		String[] lItems={"Item 1","Item 2","Item 3" };
		UiPopupMenu lMenu=new UiPopupMenu(this, lItems);
		lMenu.setAlignView(lBtnMenu, UIPopUpMenu_ALIGN.BOTTOM);
		
		lMenu.setOnPopupMenuItemClickListener(new OnPopupMenuItemClickListener() {
			@Override
			public void onPopupMenuItemClick(View v, int position, int id) {
				
				switch (position) {
				case 0:
					Toast.makeText(Sample_ActionBar.this, "Item 1", Toast.LENGTH_SHORT).show();
					break;
				case 1:
					Toast.makeText(Sample_ActionBar.this, "Item 2", Toast.LENGTH_SHORT).show();
					break;
				case 2:
					Toast.makeText(Sample_ActionBar.this, "Item 3", Toast.LENGTH_SHORT).show();
					break;
				}
			}
		});
		lMenu.show();
	}
	@Override
	public void onActionBarClick(int id) {

		switch (id) {
		case ID_CANCEL:
			Toast.makeText(this, "Action child CANCEL", Toast.LENGTH_SHORT).show();
			break;

		case ID_DONE:
			Toast.makeText(this, "Action child DONE", Toast.LENGTH_SHORT).show();
			break;
			
		case ID_MENU:
			showMenu();
			break;
		case ID_USER:
			Toast.makeText(this, "Action child USER", Toast.LENGTH_SHORT).show();
			break;
			
		default:
			break;
		}

	}

}
